/**
 * Created by adeline on 2017/7/10.
 */
import Vue from 'vue'
/**
 * 远程接口数据访问对象
 *
 * @class common.remoteDAO
 */
const RemoteDAO = {}
const Bridge = window.Bridge || {}
const decrypt = window.decrypt || {}

/**
 * 登录
 * @param callback
 */
RemoteDAO.callNativeLogin = function () {
  console.log('gtjayyz://jumpfunc?funid=30011')
  return new Promise((resolve, reject) => {
    Bridge.callByJsUrl('gtjayyz://jumpfunc?funid=30011', response => {
      if (Number(response.error) === 1) {
        console.log(response.data)
        reject(new Error(response.data))
      } else {
        console.log('原生登录-data：', response)
        resolve(Object.assign({}, response))
      }
    })
  })
}

/**
 * 获取用户自选股和持仓详情
 * @return {function}        回调
 */
RemoteDAO.getStockAndHold = function () {
  return new Promise((resolve, reject) => {
    Bridge.callByJsUrl('gtjanormal://getMyStockHoldList', response => {
      if (response.error === '1') {
        reject(new Error('get Stock And Hold error' + JSON.stringify(response)))
      } else {
        console.log('get Stock And Hold', response)
        resolve(Object.assign({}, response))
      }
    })
  })
}

/**
 * 实时行情调用
 * @return {function}        回调
 */
RemoteDAO.getStockName = function (stockCode) {
  return new Promise((resolve, reject) => {
    Bridge.callByJsUrl('gtjanrng://liveStockPrice?stockcode=' + stockCode, response => {
      if (response.error === '1') {
        console.log('get StockName-error' + JSON.stringify(response))
        reject(new Error(response.data))
      } else {
        console.log('get StockName-data:', response.data)
        resolve(Object.assign({}, response))
      }
    })
  })
}

/**
 * 相似K线明细数据
 * @param args
 * @param callback
 * args{STOCK_CODE,MARKET_CODE,DATE_NUM,THRESHOLD,REQ_NUM,PRE_DATE_NUM,POST_DATE_NUM,SIMI_RANK_START}
 */
RemoteDAO.callUC12005211 = function (args) {
  return new Promise((resolve, reject) => {
    Bridge.callByJS({
      callback: function (response) {
        if (response.error === '1') {
          console.log('12005211-相似K线明细数据-error:' + JSON.stringify(response))
          reject(new Error(response.data))
        } else {
          let result = decrypt.json2(response.data)
          let json = JSON.parse(result)
          response.data = json

          console.log('12005211-相似K线明细数据-data:', response)
          resolve(Object.assign({}, response, { data: json }))
        }
      },
      'id': 'xskx',
      'tranCode': 'UC12005211',
      'isEncrypt': 'false',
      'transmitMode': 'get',
      'param': {
        'FUNC_ID': '12005211',
        'OP_BRANCH_NO': '0',
        'OP_SITE': '127.0.0.1',
        'OP_PROGRAM': '',
        'BRANCH_NO': '0',
        'PASSWORD': '',
        'TOKEN_SERIAL_NO': '0',
        'USER_CODE': '0',
        'USER_TOKEN': '',
        'STOCK_CODE': (args.stockCode || '') + '',
        'MARKET_CODE': args.marketCode + '',
        'DATE_NUM': (args.dateNum || '') + '',
        'THRESHOLD': (args.threshold || '') + '',
        'REQ_NUM': args.reqNum + '',
        'PRE_DATE_NUM': args.preDateNum + '',
        'POST_DATE_NUM': args.postDateNum + '',
        'SIMI_RANK_START': (args.simiRankStart || '') + ''
      }
    })
  })
}

/**
 * 个股相似K线统计值
 * @param args
 * @param callback
 * args{STOCK_CODE,MARKET_CODE,DATE_NUM,THRESHOLD}
 */
RemoteDAO.callUC12005212 = function (args) {
  return new Promise((resolve, reject) => {
    Bridge.callByJS({
      callback: function (response) {
        if (response.error === '1') {
          console.log('12005212-个股相似K线统计值-error:' + JSON.stringify(response))
          reject(new Error(response.data))
        } else {
          let result = decrypt.json2(response.data)
          let json = JSON.parse(result)
          response.data = json

          console.log('12005212-个股相似K线统计值-data:', response)
          resolve(Object.assign({}, response, { data: json }))
        }
      },
      'id': 'xskx',
      'tranCode': 'UC12005212',
      'isEncrypt': 'false',
      'transmitMode': 'get',
      'param': {
        'FUNC_ID': '12005212',
        'OP_BRANCH_NO': '0',
        'OP_SITE': '127.0.0.1',
        'OP_PROGRAM': '',
        'BRANCH_NO': '0',
        'PASSWORD': '',
        'TOKEN_SERIAL_NO': '0',
        'USER_CODE': '0',
        'USER_TOKEN': '',
        'STOCK_CODE': args.stockCode + '',
        'MARKET_CODE': args.marketCode + '',
        'DATE_NUM': (args.dateNum || '') + '',
        'THRESHOLD': (args.threshold || '') + ''
      }
    })
  })
}

/**
 * 今日最相似K线
 * @param args
 * @param callback
 * args{RECOMM_TYPE,REQ_NUM}
 */
RemoteDAO.callUC12005213 = function (args) {
  return new Promise((resolve, reject) => {
    Bridge.callByJS({
      callback: function (response) {
        if (response.error === '1') {
          console.log('12005213-今日最相似K线-error:' + JSON.stringify(response))
          reject(new Error(response.data))
        } else {
          let result = decrypt.json2(response.data)
          let json = JSON.parse(result)
          response.data = json

          console.log('12005213-今日最相似K线-data:', response)
          resolve(Object.assign({}, response, { data: json }))
        }
      },
      'id': 'xskx',
      'tranCode': 'UC12005213',
      'isEncrypt': 'false',
      'transmitMode': 'get',
      'param': {
        'FUNC_ID': '12005213',
        'OP_BRANCH_NO': '0',
        'OP_SITE': '127.0.0.1',
        'OP_PROGRAM': '',
        'BRANCH_NO': '0',
        'PASSWORD': '',
        'TOKEN_SERIAL_NO': '0',
        'USER_CODE': '0',
        'USER_TOKEN': '',
        'RECOMM_TYPE': (args.recommType || '') + '',
        'REQ_NUM': (args.reqNum || '') + ''
      }
    })
  })
}

/**
 * 将需要分享的数据传送到后台接口
 * @param pagename
 * @param callback
 */
RemoteDAO.sendShareData = function (pagename) {
  return new Promise((resolve, reject) => {
    let shareContent = localStorage.getItem(pagename)
    let usercode = parseInt(Math.random() * 100000)
    let url = 'https://dl.app.gtja.com/yyz/data/api/niurenshare/share'
    Vue.prototype.$http.post(url, {usercode: usercode, type: pagename, content: shareContent})
      .then(result => {
        resolve(result)
      })
      .catch(error => reject(new Error(error)))
  })
}

/**
 *  分享
 * @param title     分享题目
 * @param content   分享的内容
 * @param linkurl   链接
 * @param moments 表示分享到朋友圈是使用title还是content,或是都拼在一起  title/content/all
 * @param icon 表示分享出去的图片地址
 */
RemoteDAO.share = function (args) {
  console.log(args)
  window.location.href = 'gtjayyznormal://yyzShare?title=' + args.title + '&icon=' + args.icon + '&content=' + args.content + '&moments=' + args.moments + '&linkurl=' + args.linkurl
}

export default RemoteDAO

